module.exports = {
    /*
    * 验证手机号 是否合法
    * */
    isPhoneNumber: function (tel) {
        return (/^[1][3,4,5,6,7,8,9][0-9]{9}$/.test(tel))
    },

    /*
    * 验证信息
    * 字符串不为空 且 不为空格
    * 如果为空或者空白 返回fasle
    * */
    isInputEmpty: function (str) {
        let reg = "^[ ]+$";
        let re = new RegExp(reg);
        if (str === "" || typeof(str) === "undefined") {
            return false
        } else if (re.test(str)){
            return false
        }
        return true
    },

    /*
    * 验证格式邮箱
    */
    isEmail: function(email) {
        const reg = new RegExp("^[a-z0-9A-Z]+[- | a-z0-9A-Z . _]+@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-z]{2,}$");
        return reg.test(email)
    },

    /*
    * 类型判断
    * */
    isType: function (value, type) {
        return Object.prototype.toString.call(value) === "[object " + type + "]"
    },

    /*
     * 返回wx.navigateBack 层级
     * 根据pageUrl页面路径参数 查找pages栈中存在相同路径的层级数
     * 如果传入的参数为空  则返回首页
     * 如果page栈中存在传入的路径参数 则返回该页面在page中所在的位置
     * 如果栈中没有找到匹配路径  跳转到该路径页面
     * */
    navigatePage: function (url, isRedirectTo) {
        const pages = getCurrentPages();
        let currentRouteInx = pages.length - 1;
        let currentIndex = "";
        let pageUrl = url && url !== "undefined" ? (url.substring(0, 1) === "/" ? url : ("/" + url)) : "";
        if (pageUrl) {
            for (let i = 0; i < pages.length; i ++) {
                if (pageUrl.split("?")[0] === ("/" + pages[i].route)) {
                    currentIndex = currentRouteInx - i;
                    break
                }
            }
            setTimeout(function () {
                if (currentIndex) {
                    wx.navigateBack({delta: currentIndex})
                } else if (!!isRedirectTo) {
                    wx.redirectTo({url: pageUrl})
                } else {
                    wx.navigateTo({url: pageUrl})
                }
            }, 300);
        } else if (pages.length > 1) {
            setTimeout(function () {wx.navigateBack()}, 300);
        } else {
            wx.redirectTo({url: "/pages/login/index/login"})
        }
    },


    /*
    *时间戳转换成日期函数
    * isDetail来判断是否显示详细
    * 待优化
    * */
    timesToDate: function(t, isDetail = false) { //t是要转的13位时间戳
        let str = typeof (t) !== "number" && t.indexOf ("-") !== -1 ? t.replace(/-/g, "/") : t;
        let date = new Date(str),
            Y = date.getFullYear() + "-",
            M = (date.getMonth()+1 < 10 ? "0"+(date.getMonth()+1) : date.getMonth()+1) + "-",
            D = parseInt(date.getDate()) < 10 ? ("0" + date.getDate() + " ") : date.getDate(),
            h = date.getHours() < 10 ? ("0" + date.getHours() + ":") : date.getHours() + ":",
            m = date.getMinutes() < 10 ? ("0" + date.getMinutes() + ":") : date.getMinutes() + ":",
            s = date.getSeconds() < 10 ? ("0" + date.getSeconds()) : date.getSeconds();

        if (!isDetail) {return Y + M + D}
        return Y + M + D + " " + h + m + s;
    },

    /*
    * 将对象参数拼接字符串(放入url之后的参数)
    * */
    queryToString: function (params = {}) {
        if (Object.keys(params).length > 0) {
            let str = "";
            for (let i in params) {
                str += (i + "=" + params[i] + "&")
            }
            str = str.substr(0, str.length - 1);
            return "?" + str
        } else {
            return ""
        }
    },
}